package cn.hfatec.shms.web.yk.ykUser.service;

import java.util.Date;
import java.util.List;
import java.util.HashMap;


import cn.hfatec.shms.advice.annotation.MysqlDataSource;
import cn.hfatec.shms.enums.KeyTypeEnum;
import cn.hfatec.shms.exception.KeyGenException;
import cn.hfatec.shms.system.user.model.User;
import cn.hfatec.shms.utils.KeyGenUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hfatec.base.BaseDao;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.shms.web.yk.ykUser.model.YkUser;
import cn.hfatec.shms.web.yk.ykUser.dao.YkUserDao;
import cn.hfatec.shms.web.yk.ykUser.model.YkUserParams;

import javax.annotation.Resource;



/**
 *
 * @author hfatec
 * @version 1.0
 *
 * the JAVA code is generate by middlegen
 *
 */
@Service("YkUserService")
public class YkUserService {

    public static final String BEAN_ID = "YkUserService";

    //注入Dao层
    @Resource(name = "YkUserDao")
    private YkUserDao ykUserDao;

    //注入BaseDao
    @Resource(name = "BaseDao")
    private BaseDao baseDao;

    /**
     *
     * 查询数据
     *
     * @param ykUserParams
     * @param page
     *
     */
    @Transactional
    @MysqlDataSource
    public Table queryYkUserTable(YkUserParams ykUserParams, Page page){

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("params", ykUserParams);
        return baseDao.queryTableByPage("queryYkUser", params, page);
    }

    /**
     *
     * 插入数据
     * @param ykUser
     *
     */
    @Transactional
    @MysqlDataSource
    public void saveYkUser(YkUser ykUser,User user) throws KeyGenException {
        if(ykUser.getId() != null) {
            YkUser result = getYkUser(ykUser.getId());
            if(result != null) {
                ykUserDao.update(ykUser);
            } else {
                if(ykUser.getUserId()==null)
                ykUser.setUserId(KeyGenUtils.createKeyByType(KeyTypeEnum.USER_KEY));
                ykUser.setCreateTime(new Date());
                ykUser.setCreateUser(user.getLoginName());
                ykUserDao.insert(ykUser);
            }
        } else {
            if(ykUser.getUserId()==null)
            ykUser.setUserId(KeyGenUtils.createKeyByType(KeyTypeEnum.USER_KEY));
            ykUser.setCreateTime(new Date());
            ykUser.setCreateUser(user.getLoginName());
            ykUserDao.insert(ykUser);
        }
    }

    /**
     *
     * 删除数据
     * @param id
     *
     */
    @Transactional
    @MysqlDataSource
    public void deleteYkUser(Integer id) {

        ykUserDao.delete(id);
    }

    /**
     *
     * 删除数据
     * @param ykUserList
     *
     */
    @Transactional
    @MysqlDataSource
    public void deleteYkUserBatch(List<String> ykUserList) {
        for(String id : ykUserList) {
            deleteYkUser(Integer.valueOf(id));
        }
    }

    /**
     *
     * 根据ID获取数据
     * @param id
     *
     */
    @Transactional
    @MysqlDataSource
    public YkUser getYkUser(Integer id) {

        return ykUserDao.getYkUserById(id);
    }
    /**
     *
     * 根据ID获取数据
     * @param staffId
     *
     */
    @Transactional
    @MysqlDataSource
    public YkUser getYkUserStaffId(String staffId) {

        return ykUserDao.getYkUserByStaffId(staffId);

    }
}